package rabbit.flt.core;

import rabbit.flt.common.log.AgentLoggerFactory;
import rabbit.flt.common.log.Logger;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * 插件类加载器
 */
public abstract class PluginClassLoader {

    protected Logger logger = AgentLoggerFactory.getLogger(getClass());

    private static PluginClassLoader pluginClassLoader;

    public static PluginClassLoader getPluginClassLoader() {
        return pluginClassLoader;
    }

    public static void setPluginClassLoader(PluginClassLoader pluginClassLoader) {
        PluginClassLoader.pluginClassLoader = pluginClassLoader;
    }

    /**
     * 加载类
     *
     * @param name
     * @return
     * @throws ClassNotFoundException
     * @throws IOException
     */
    public abstract Class<?> loadClassByName(String name) throws IOException, ClassNotFoundException, URISyntaxException;

    /**
     * 获取代理类路径
     *
     * @return
     */
    protected String getAgentJarFilePath() throws MalformedURLException, URISyntaxException {
        String classResourcePath = AgentEntry.class.getName().replaceAll("\\.", "/").concat(".class");
        String url = ClassLoader.getSystemClassLoader().getResource(classResourcePath).toString();
        url = url.substring(url.indexOf("file:"), url.indexOf('!'));
        return new File(new URL(url).toURI()).getPath();
    }

}
